package com.heima.article.listener;

import com.alibaba.fastjson.JSON;
import com.heima.article.dto.ArticleStreamMessage;
import com.heima.article.service.IHotArticleService;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.stereotype.Service;

@Service
public class ArticleResultListener {

    @Autowired
    IHotArticleService hotArticleService;

    @KafkaListener(topics = "article_result")
    public void handleMessage (ConsumerRecord<String,String> record) {
        System.out.println("收到消息" + record.value());
        //将消息转化成对象
        ArticleStreamMessage message = JSON.parseObject(record.value(), ArticleStreamMessage.class);
        //根据本次聚合的消息更新
        hotArticleService.update(message);
    }
}
